﻿ 
using Nop.Core;
using Nop.Core.Infrastructure;
using Nop.Core.Plugins;
using Nop.Plugin.Resources.Client.Data;
using Nop.Plugin.Resources.Client.Domain;
using Nop.Plugin.Resources.Client.Helpers;
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Web.Framework.Menu;
using System;
using System.Collections.Generic;
using System.Text;

namespace Nop.Plugin.Resources.Client
{
    public class ResPlugin : BasePlugin, IAdminMenuPlugin
    {
        //private readonly IWebConfigMangerHelper _webConfigMangerHelper;
        private readonly ResObjectContext _objectContext;
        private readonly ISettingService _settingService;
        private readonly IWorkContext _workContext;
        private readonly IWebHelper _webHelper;
        private readonly ILocalizationService _localizationService;

        public ResPlugin(ResObjectContext objectContext,/*IWebConfigMangerHelper webConfigMangerHelper,*/ ISettingService settingService, IWorkContext workContext,
          ILocalizationService localizationService, IWebHelper webHelper
/*, IConfiguration configuration*/)
        {
            _objectContext = objectContext;
            //_webConfigMangerHelper = webConfigMangerHelper;
            _settingService = settingService;
            _workContext = workContext;
            _localizationService = localizationService;
            _webHelper = webHelper;
            //_configuration = configuration;
        }


        public override void Install()
        {
            var configManagerHelper = new NopConfigManagerHelper();
            _objectContext.Install();

            ResSettings settings = new ResSettings
            {
                Enable = true,
                EnableLogging = false,
                HostUrl = "",
            };

            _settingService.SaveSetting(settings);

            base.Install();
        }


        public override void Uninstall()
        {
            _objectContext.Uninstall(); 

            base.Uninstall();
        }


        public void ManageSiteMap(SiteMapNode rootNode)
        {
            string pluginMenuName = _localizationService.GetResource("Plugin.Resources.Client.Admin.Menu.Title", languageId: _workContext.WorkingLanguage.Id, defaultValue: "资源服务器");
            string settingsMenuName = _localizationService.GetResource("Plugin.Resources.Client.Admin.Menu.Settings.Title", languageId: _workContext.WorkingLanguage.Id, defaultValue: "设置");

            const string adminUrlPart = "Admin/";

            var pluginMainMenu = new SiteMapNode
            {
                Title = pluginMenuName,
                Visible = true,
                SystemName = "Res-Main-Menu",
                IconClass = "fa-genderless"
            };

            pluginMainMenu.ChildNodes.Add(new SiteMapNode
            {
                Title = settingsMenuName,
                Url = _webHelper.GetStoreLocation() + adminUrlPart + "ResAdmin/Settings",
                Visible = true,
                SystemName = "Res-Settings-Menu",
                IconClass = "fa-genderless"
            }); 

            rootNode.ChildNodes.Add(pluginMainMenu);
        }
    }
}
